/*
 * uscore.js
 * Copyright(c) 2012 caih
 *
 */

// Dependencies:
// 1) jQuery
// 2) browser.js

//websocket
var ws = null;
var target='ws://' + window.location.host+'/examples/websocket/echoMessage';


function connect() 
{            
	if ('WebSocket' in window) 
	{
		ws = new WebSocket(target);
	} 
	else if ('MozWebSocket' in window) 
	{
		ws = new MozWebSocket(target);
	} 
	else 
	{
		alert('WebSocket is not supported by this browser.');
		return;
  };
  
	ws.onopen = function () 
	{
     log('Info: WebSocket connection opened.');
      sendMsg( "----ok---");
  };
	ws.onmessage = function (event) 
	{
		log('Received-----: ' + event.data);
	};
	ws.onclose = function () 
	{
		log('Info: WebSocket connection closed.');
	};
}

function disconnect() 
{
	if (ws != null) 
	{
		ws.close();
		ws = null;
	}          
}

function sendMsg(msg) 
{
  if (ws != null)
  {
     log('Sent: ' + msg);
     ws.send(msg);
  } 
  else
  {
     alert('WebSocket connection not established, please connect.');
  }
}

function log(message) 
{
	console.log(message);
}

//core
var reqStr='[{"tag":"pscada.kg.val","ref":"6210001,12" },{"tag":"pscada.kg1.val","ref":"6210002,12" }] ';
var dataStr= '[{"tag":"pscada.kg.val","ref":"6210001,12","val":"","state":""},{"tag":"pscada.kg1.val","ref":"6210002,12","val":"","state":""}] ';
 
var colorStr='{	"kgcolor": {"v0":{"lc":"#000000","fc":"#000000"},"v1":{"lc":"#FFFF00","fc":"#FF0000"}},	"dzcolor": {"v1":{"lc":1231,"fc":666},"v2":{"lc":31,"fc":66}}		 }';
// var colorStr='{	"kgcolor": {"v0":{"lc":1231,"fc":666} } }';

 function HtmlEncode(s)
{
	if (s==null) return s;
	var ret =s.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;");
	return ret;
}
function HtmlDecode(s)
{    
  	if (s==null) return s;
  	var ret = s.replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&quot;/g, '"').replace(/&amp;/g, '&');    
  	return ret;
};

function getVal(x1)
{
	return 1;
}

function fun1(x1)
{
	return parseInt(x1);
	//return parseInt(x1)+100;
}

function animObj(item,info)
{
	      var tags=[{"tag":"pscada.tag1","value":"0","statue":"128"},{"tag":"pscada.tag2","value":"1","statue":"228"}];
	      var gg=HtmlDecode(info);
	      var obj=JSON.parse(gg);
	      this.item=item;
        /*var tmp= obj.src.split(";"); //map
        for(int i=0;i<tmp.length;i++) 
        {
        	tags.push({"tag":tmp[i],"value":"0","statue":"0"});
        }
        this.fun=obj.fun;
        this.colSet=obj.colSet;
        
        this.introduceSelf = function(){
            window.alert("I am a " + this.src+"!");
        };*/
}


(function(){
	alert("init------------");
	window.onload = init;

})()

function init()
{	 
	 $("svg *").each(function(){
  	 if($(this).attr("id")!= undefined)
  	 {  	 	
  	 		var anim=$(this).attr("s_anim_gen");
  	 	//alert("00000000000"+anim);
  	 		if(anim!= undefined)
				{
  	 			var animobj=new animObj($(this),anim);  	 		
  	 		}
     }
  });
 
}

function render()
{
	//init websocket
	sendMsg(reqStr);
	//connect();
	//sendMsg("---req---");
	alert("gggg");
  $("svg *").each(function(){
  	 if($(this).attr("id")!= undefined)
  	 {
  	 	
  	 	var anim=$(this).attr("s_genanim");
  	 	//alert("00000000000"+anim);
  	 
  	 	var output =htmlDecode(anim);
  	 	
  	 	var myObject =  jQuery.parseJSON(output);
  	  var color=myObject.clrset;
      //alert($(this).attr("id")+"---"+myObject.con+"--"+myObject.fun+"--"+color);
      var val1=getVal(myObject.con);
      var ret=eval(myObject.fun+"("+val1+")");
      //alert("ret="+ret);
  
      
      var colorObj =  jQuery.parseJSON(colorStr);
      var lc="colorObj."+color+".v"+val1+".lc";
      var lcolor=eval(lc);
      $(this).attr("fill",lcolor);
      //alert(v1);
      //alert(v1+"="+eval(v1));
     }
  });
}

 